'''
Created on Dec 8, 2015

@author: JimmyMo
'''
# from resourcecare.configuration.confignode import ConfigNode
import os.path

# def collectFilePath_decorator(func):
#     def collectFilePath(collector):
#         for locator in collector.locators:
#             inputs_iter = locator.do_locate()
#             outputs = [elem for elem in inputs_iter]
#             collector.outputs += outputs
#         func()
#     return collectFilePath();
# 
# def collectFilePath(collector):
#     for locator in collector.locators:
#         inputs_iter = locator.do_locate()
#         outputs = [elem for elem in inputs_iter]
#         collector.outputs += outputs
# #     return collector.outputs   
# @collectFilePath_decorator
# def collectFileName(collector):
#     inputs = collector.parentCollector.do_collect()
#     [collector.outputs.append(os.path.basename(elem)) for elem in inputs]
#     return collector.outputs
#     
# def collectFileSize(collector):
#     pass
class Collector(object):
    def __init__(self, enabled, name, parentCollector, desc, locators):
        self.enabled = enabled
        self.name = name
        self.parentCollector = parentCollector
        self.desc = desc
        self.locators = locators
        self.outputs = []

class FilePathCollector(Collector):
    def __init__(self, enabled, name, desc, locators):
        super().__init__(enabled, name, None, desc, locators)

    def do_collect(self):
#         [self.outputs +=  for locator in self.locators for elem in locator.do_locate()]
        for locator in self.locators:
            inputs_iter = locator.do_locate()
#             [self.outputs.append(elem) for elem in inputs]
            outputs = [elem for elem in inputs_iter]
            self.outputs += outputs

        return self.outputs   

class FileNameCollector(Collector):
    def __init__(self, enabled, name, desc, locators):
        parentCollector = FilePathCollector(True, 'filepathcollector', 'desc...', locators)
        super().__init__(enabled, name, parentCollector, desc, locators)
    
    def do_collect(self):
        inputs = self.parentCollector.do_collect()
        [self.outputs.append(os.path.basename(elem)) for elem in inputs]
        return self.outputs

def getCollector(collectorName, locators):
    collectorCls = globals()[collectorName]
    collector = collectorCls(True, collectorName, "parent collector", locators)
    return collector

class RegularExpressionCollector(Collector):
    def __init__(self, enabled, name, parentCollectorName, desc, locators, ruleProcessor):
        parentCollector = getCollector(parentCollectorName, locators)
#         parentCollector = globals()[parentCollectorName](enabled, 
#                                                          parentCollectorName, 
#                                                          'inside RegularExpressionCollector', 
#                                                          locators)
        super().__init__(enabled, name, parentCollector, desc, locators)
        self.ruleProcessor = ruleProcessor
     
    def do_collector(self):
        inputs = self.parentCollector.do_collect()
        outputs = [self.ruleProcessor.do_process(elem) for elem in inputs]
        self.outputs = outputs
        return self.outputs